/*
 * Copyright (C) 2012 SeekDeepWithin.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package com.seekdeepwithin.Controls;

import javafx.beans.value.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.Rectangle;

/**
 * User: Jonathan Montiverdi
 * Date: Oct 12, 2012
 *
 * Divider for a split panel.
 */
public class Divider extends Rectangle
{
    /**
     * Initializes a new divider.
     * @param pane The pane the divider is a child of.
     * @param orientation Orientation of the divider.
     */
    public Divider (final Pane pane, final Orientation orientation)
    {
        this.setStrokeWidth (1);
        this.setStroke (Color.GRAY);
        this.setFill (Color.GRAY);
        Stop[] stops = new Stop[] { new Stop(0, Color.WHITE), new Stop(0.5, Color.LIGHTGRAY), new Stop(1, Color.GRAY)};
        LinearGradient gradient = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
        this.setFill (gradient);

        setOnMouseEntered (new EventHandler<MouseEvent> () {
            @Override public void handle (MouseEvent t)
            {
                if (orientation == Orientation.HORIZONTAL)
                    setCursor (Cursor.H_RESIZE);
                else
                    setCursor (Cursor.V_RESIZE);
            }
        });

        setOnMouseExited (new EventHandler<MouseEvent> () {
            @Override public void handle (MouseEvent t)
            {
                setCursor (Cursor.DEFAULT);
            }
        });

        setOnMouseDragged (new EventHandler<MouseEvent> () {
            @Override public void handle (MouseEvent me)
            {
                if (orientation == Orientation.HORIZONTAL)
                {
                    Region node = (Region)pane.getChildren ().get (0);
                    double width = node.getWidth () + me.getX ();
                    if (width < 0) width = 0;
                    node.setMinWidth (width);
                    node.setMaxWidth (width);
                }
                else
                {
                    Region node = (Region)pane.getChildren ().get (0);
                    double height = node.getHeight () + me.getY ();
                    if (height < 0) height = 0;
                    node.setMinHeight (height);
                    node.setMaxHeight (height);
                }
            }
        });

        if (orientation == Orientation.HORIZONTAL)
        {
            pane.heightProperty ().addListener (new ChangeListener () {
                @Override public void changed (ObservableValue ov, Object oldValue, Object newValue)
                {
                    setHeight ((Double)newValue - 1);
                }
            });
        }
        else
        {
            pane.widthProperty ().addListener (new ChangeListener () {
                @Override public void changed (ObservableValue ov, Object oldValue, Object newValue)
                {
                    setWidth ((Double)newValue - 1);
                }
            });
        }
    }
}
